package org.sjm.samples.service;

import java.util.ArrayList;
import java.util.List;

import org.sjm.samples.dao.ISubjectDAO;
import org.sjm.samples.dao.IUserSubjectDAO;
import org.sjm.samples.model.ExamSubjectVO;
import org.sjm.samples.model.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SubjectServiceImpl implements ISubjectService {

	@Autowired
	private ISubjectDAO subjectDAO;
	
	@Autowired
	private IUserSubjectDAO userSubjectDAO;
	
	@Override
	public List<ExamSubjectVO> findAllSubjects() {
		List<Subject> subjectList = subjectDAO.FindAllSubjects();
		List<ExamSubjectVO> voList = new ArrayList<ExamSubjectVO>();
		for(Subject subject : subjectList){
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public ExamSubjectVO getSubjectById(Integer subId) {
		Subject subject = subjectDAO.getSubjectById(subId);
		ExamSubjectVO vo = new ExamSubjectVO();
		BeanUtils.copyProperties(subject, vo);
		return vo;
	}

	@Override
	public Integer editSubject(ExamSubjectVO subjectVO) throws Exception {
		Subject subject = new Subject();
		BeanUtils.copyProperties(subjectVO, subject);
		subjectDAO.editSubject(subject);
		return 1;
	}

	@Override
	public Integer createSubject(ExamSubjectVO subjectVO) throws Exception {
		Subject subject = new Subject();
		BeanUtils.copyProperties(subjectVO, subject);
		subjectDAO.createSubject(subject);
		return 1;
	}

	@Override
	public Integer deleteSubject(Integer subId) throws Exception {
		userSubjectDAO.deleteUserSubjectBySubId(subId);
		subjectDAO.deleteSubject(subId);
		return 1;
	}

}
